package com.apache.website.controller;

import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.apache.api.manager.BaseManager;
import com.apache.api.vo.ParamsVo;
import com.apache.api.vo.ResultMsg;
import com.apache.api.vo.Token;
import com.apache.database.constant.Validator;
import com.apache.tools.DateUtils;
import com.apache.uct.common.BaseAction;
import com.apache.website.entity.Channel;
import com.apache.website.entity.Manuscript;
import com.apache.website.manager.ChannelManager;
import com.apache.website.manager.ManuscriptManager;

@Controller
@RequestMapping("wcm/channel/")
public class ChannelAction extends BaseAction<Channel> {

	@Autowired
	private ChannelManager channelManager;
	@Autowired
	private ManuscriptManager manuscriptManager;

	protected BaseManager<Channel> getBaseManager() {
		return channelManager;
	}

	protected String getPrefix() {
		return "website/channel-";
	}

	@RequestMapping("list!data.action")
	@ResponseBody
	protected Object data(HttpServletRequest request, String page, String rows) {
		String parentId = request.getParameter("id");
		Channel org = new Channel();
		if (Validator.isNull(parentId)) {
			parentId = "0";
		}
		org.setParentId(parentId);
		ParamsVo<Channel> vo = new ParamsVo<Channel>();
		vo.setObj(org);
		List<Channel> list = channelManager.getList(vo);
		net.sf.json.JSONArray jary = new net.sf.json.JSONArray();
		int count = list.size();
		for (int i = 0; i < count; i++) {
			Channel orgVo = list.get(i);
			net.sf.json.JSONObject jvo = net.sf.json.JSONObject.fromObject(orgVo);
			//			if (null != orgVo.getSubCount() && orgVo.getSubCount() > 0) {
			jvo.put("state", "closed");
			//			} else {
			//				jvo.put("state", "opened");
			//			}
			jary.add(jvo);
		}
		return jary;
	}

	/**
	 * 功能说明：跳转到新增页面
	 */
	@Token(save = true)
	@RequestMapping(value = "add.action", method = RequestMethod.GET)
	protected ModelAndView add(HttpServletRequest request) {
		ModelAndView view = new ModelAndView(EDIT);
		ParamsVo<Channel> vo = new ParamsVo<Channel>();
		Channel org = null;
		if (Validator.isNotNull(request.getParameter("channelId"))) {
			vo.setInfoId(request.getParameter("channelId"));
			org = (Channel) getBaseManager().getInfoById(vo);
		} else {
			org = new Channel();
			org.setParentId(request.getParameter("parentId"));
		}
		view.addObject("item", org);
		return view;
	}

	/**
	 * TODO 保存栏目信息.  
	 */
	@Token(remove = true)
	@RequestMapping(value = "/save.action", method = RequestMethod.POST)
	@ResponseBody
	public ResultMsg save(HttpServletRequest request, HttpServletResponse response, @ModelAttribute Channel item) {
		ResultMsg rmsg = new ResultMsg("T", "操作成功！");
		ParamsVo<Channel> vo = getParamsVo(request);
		if (Validator.isNull(item.getParentId()))
			item.setParentId("0");
		if (Validator.isNotNull(item.getChannelId())) {//对栏目信息修改
			vo.setObj(item);
			boolean mark = getBaseManager().editInfo(vo);
			if (!mark) {
				rmsg = getResultMsg(request, "栏目信息修改失败！");
			}
		} else {//添加栏目信息
			item.setCreateUser(this.getSessionUser(request).getUserEname());
			item.setCreateTime(DateUtils.format(new Date(), "yyyy-MM-dd HH:mm:ss"));
			vo.setObj(item);
			vo.setKey("checkChannelCode");
			String flag = String.valueOf(channelManager.execute(vo));
			if ("true".equals(flag)) {
				String id = getBaseManager().saveInfo(vo);
				if (Validator.isNull(id)) {
					rmsg = getResultMsg(request, "栏目信息添加失败！");
				}
			} else {
				rmsg = getResultMsg(request, "栏目标识[" + item.getCodeName() + "]已经被使用");
			}
		}
		return rmsg;
	}

	//删除栏目信息
	@RequestMapping("del.action")
	@ResponseBody
	public ResultMsg delete(HttpServletRequest request, String id) {
		ResultMsg rmsg = new ResultMsg("T", "删除成功！");
		ParamsVo<Channel> vo = getParamsVo(request);
		if (isNotNull(id)) {
			ParamsVo<Manuscript> mvo = new ParamsVo<Manuscript>();
			mvo.setKey("manuscriptsByChannelId");
			mvo.setInfoId(id);
			long count = Long.valueOf(String.valueOf(manuscriptManager.execute(mvo)));
			if (count > 0) {
				rmsg.setFlag("F");
				rmsg.setMsg("请先删除此栏目下的稿件");
				return rmsg;
			}
			vo.setInfoId(id);
			boolean mark = channelManager.deleteInfo(vo);
			if (!mark) {
				rmsg.setFlag("F");
				rmsg.setMsg("删除失败");
			}
		} else {
			rmsg.setFlag("F");
			rmsg.setMsg("请选择要删除的栏目！");
		}
		return rmsg;
	}
}
